package com.example.lib_base.mvi

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class TaskViewModel : ViewModel() {
    private val tasks = mutableListOf<String>() // 假设任务是字符串
    private val _state = MutableLiveData<List<String>>()
    val state: LiveData<List<String>> = _state

    fun handleIntent(intent: TaskIntent) {
        when (intent) {
            is TaskIntent.LoadTasks -> loadTasks()
            is TaskIntent.AddTask -> addTask(intent.task)
            is TaskIntent.UpdateTask -> updateTask(intent.taskId, intent.newTask)
            is TaskIntent.DeleteTask -> deleteTask(intent.taskId)
        }
    }

    private fun loadTasks() {
        // 加载任务逻辑
        _state.value = tasks // 更新状态
    }

    private fun addTask(task: String) {
        tasks.add(task)
        _state.value = tasks // 更新状态
    }

    private fun updateTask(taskId: Int, newTask: String) {
        if (taskId in tasks.indices) {
            tasks[taskId] = newTask
            _state.value = tasks // 更新状态
        }
    }

    private fun deleteTask(taskId: Int) {
        if (taskId in tasks.indices) {
            tasks.removeAt(taskId)
            _state.value = tasks // 更新状态
        }
    }
}